package com.xiaohong.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.user.dao.UserMapper;
import com.xiaohong.user.pojo.User;
import com.xiaohong.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 条件+分页查询User
     * @param user 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<User> findPage(User user, int page, int size){
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return userMapper.selectPage(pg, queryWrapper);
    }

    /**
     * 分页查询User
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<User> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return userMapper.selectPage(pg, null);
    }

    /**
     * 多条件搜索方法User
     * @param user
     * @return
     */
    @Override
    public List<User> findList(User user){
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user);
        queryWrapper.orderByDesc("gmt_create");
        // 查询数据
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除User
     * @param id
     */
    @Override
    public void delete(Integer id){
        userMapper.deleteById(id);
    }

    /**
     * 根据ID修改User
     * @param user
     */
    @Override
    public void update(User user){
        userMapper.updateById(user);
    }

    /**
     * 增加User
     * @param user
     */
    @Override
    public void add(User user){
        userMapper.insert(user);
    }

    /**
     * 根据ID查询User
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id){
        return  userMapper.selectById(id);
    }

    /**
     * 查询全部User数据
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }
}
